/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/ccjzxyxt">ccjzxyxt</a> All rights reserved.
 */
package cn.net.dckj.modules.biz.web;

import cn.net.dckj.common.config.Global;
import cn.net.dckj.common.persistence.Page;
import cn.net.dckj.common.utils.StringUtils;
import cn.net.dckj.common.web.BaseController;
import cn.net.dckj.modules.biz.entity.BizEcsSetting;
import cn.net.dckj.modules.biz.service.BizEcsSettingService;
import cn.net.dckj.modules.cms.entity.Site;
import cn.net.dckj.modules.cms.utils.CmsUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 云服务器套餐设置Controller
 *
 * @author hqj
 * @version 2019-04-09
 */
@Controller
@RequestMapping(value = "${adminPath}/biz/bizEcsSetting")
public class BizEcsSettingController extends BaseController {

    @Autowired
    private BizEcsSettingService bizEcsSettingService;

    @ModelAttribute
    public BizEcsSetting get(@RequestParam(required = false) String id) {
        BizEcsSetting entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = bizEcsSettingService.get(id);
        }
        if (entity == null) {
            entity = new BizEcsSetting();
        }
        return entity;
    }

    @RequiresPermissions("biz:bizEcsSetting:view")
    @RequestMapping(value = {"list", ""})
    public String list(BizEcsSetting bizEcsSetting, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<BizEcsSetting> page = bizEcsSettingService.findPage(new Page<BizEcsSetting>(request, response), bizEcsSetting);
        String origin = request.getParameter("origin");
        if (("front").equals(origin)) {
            model.addAttribute("page1", page);
            Site site = CmsUtils.getSite(Site.defaultSiteId());
            model.addAttribute("site", site);
            String tabIndex = request.getParameter("tabIndex");
            model.addAttribute("tabIndex", tabIndex);
            return "modules/cms/front/themes/basic/grzx_setMeal";
        } else {
            model.addAttribute("page", page);
            return "modules/biz/bizEcsSettingList";
        }
    }

    @RequiresPermissions("biz:bizEcsSetting:view")
    @RequestMapping(value = "form")
    public String form(BizEcsSetting bizEcsSetting, Model model) {
        model.addAttribute("bizEcsSetting", bizEcsSetting);
        return "modules/biz/bizEcsSettingForm";
    }

    @RequiresPermissions("biz:bizEcsSetting:edit")
    @RequestMapping(value = "save")
    public String save(BizEcsSetting bizEcsSetting, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, bizEcsSetting)) {
            return form(bizEcsSetting, model);
        }
        bizEcsSettingService.save(bizEcsSetting);
        addMessage(redirectAttributes, "保存云服务器套餐设置成功");
        return "redirect:" + Global.getAdminPath() + "/biz/bizEcsSetting/?repage";
    }

    @RequiresPermissions("biz:bizEcsSetting:edit")
    @RequestMapping(value = "delete")
    public String delete(BizEcsSetting bizEcsSetting, RedirectAttributes redirectAttributes) {
        bizEcsSettingService.delete(bizEcsSetting);
        addMessage(redirectAttributes, "删除云服务器套餐设置成功");
        return "redirect:" + Global.getAdminPath() + "/biz/bizEcsSetting/?repage";
    }
}
